<?php

/*	Vulpes-backend - Web-based managegent for wireless internet providers.
	Copyright (C) 2007,2008 LinFox Serviços em Informática LTDA.

	This file is part of Vulpes-backend.

	Vulpes-backend is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes-backend is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 	Please note that any front-end (client application) that is distributed
	along with Vulpes-backend, such as the Vulpes frontend by LinFox,
	is subject to it's correspending licene. */

require_once 'controller.php';
require_once 'plan_dao.php';
require_once 'schedule_dao.php';

/**
 * Plans Controller
 */
class plan_ctl extends controller
{
	protected $mode = 'plans';
	protected $model = 'plan';
	protected $dao = 'plan_dao';

	function __construct()
	{
		parent::__construct();
	}

	function show()
	{
		$plan_dao = new plan_dao();
		$schedule_dao = new schedule_dao();
		$plan = $plan_dao->fetch(get($plan_dao->key));
		$schedules = $schedule_dao->fetch_plan(get($plan_dao->key));
		$result = '<plan>';
		$result .= $plan->xml_iterate();
		$result .= '<schedules>';
		foreach ($schedules as $schedule) {
			$day = $schedule->day;
			$result .= '<schedule>';
			$result .= $schedule->xml_iterate();
			$result .= '<day_name>'.lang::get($day).'</day_name>';
			$result .= '</schedule>';
		}
		$result .= '</schedules>';
		$result .= '</plan>';
		
		return $result;
	}
	
	function show_all()
	{
		$plan_dao = new plan_dao($this->db);
		$plans = $plan_dao->fetch_all();

		$result .= '<plans>';
		foreach ($plans as $plan) {
			$result .= $plan->xml_iterate('plan');
		}
		$result .= '</plans>';
		$result .= '
			<days>
				<day>
					<value>all</value>
					<label>'.lang::all.'</label>
				</day>
				<day>
					<value>workday</value>
					<label>'.lang::workday.'</label>
				</day>
				<day>
					<value>weekend</value>
					<label>'.lang::weekend.'</label>
				</day>
				<day>
					<value>monday</value>
					<label>'.lang::monday.'</label>
				</day>
				<day>
					<value>tuesday</value>
					<label>'.lang::tuesday.'</label>
				</day>
				<day>
					<value>wednesday</value>
					<label>'.lang::wednesday.'</label>
				</day>
				<day>
					<value>thursday</value>
					<label>'.lang::thursday.'</label>
				</day>
				<day>
					<value>friday</value>
					<label>'.lang::friday.'</label>
				</day>
				<day>
					<value>saturday</value>
					<label>'.lang::saturday.'</label>
				</day>
				<day>
					<value>sunday</value>
					<label>'.lang::sunday.'</label>
				</day>
			</days>';
		return $result;	
	}
	
	function insert_schedule()
	{
		$schedule_dao = new schedule_dao();
		$schedule = $schedule_dao->fetch_model();
		$schedule->get_request();
		$schedule_dao->insert($schedule);
		changes::get_instance()->mark($this->mode);
		return $this->show();
	}
	
	function remove_schedule()
	{
		$schedule_dao = new schedule_dao();
		$schedule_dao->delete(get($schedule_dao->key));
		changes::get_instance()->mark($this->mode);
		return $this->show();
	}

}

?>